package com.banshuibao.bsb_android.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.ExpertRecorderDetailModel;
import com.banshuibao.bsb_android.model.RecorderContentModel;
import com.banshuibao.bsb_android.model.RecorderModel;
import com.banshuibao.bsb_android.model.RequestOkModel;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.MyEditText;
import com.banshuibao.bsb_android.util.XToast;

import java.util.List;

public class AdditionalEditActivity extends BaseActivity implements View.OnClickListener{

    private String title_tv,recorder_content;
    private TextView title;
    private RelativeLayout next;
    private MyEditText editText;
    private ExpertRecorderDetailModel.DataBean dataBean;
    private String questionUuid,voiceUuid,typeUuid;
    private boolean isRecord= false;
    private RecorderContentModel model;
    private RecorderModel recorderModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_additional_edit);
        dataBean = (ExpertRecorderDetailModel.DataBean) getIntent().getSerializableExtra("bean");
        title_tv = dataBean.getExpertQu().getQuestion();
        questionUuid = dataBean.getExpertQu().getUuid();
        initView();
    }
    public void initView() {
        setTitleText("修改补录录音");
        title = findViewById(R.id.title);
        editText = findViewById(R.id.content);
        next = findViewById(R.id.next);
        next.setOnClickListener(this);
        model = new RecorderContentModel();
        recorderModel = new RecorderModel();
        setData();
    }
    public void setData(){
        title.setText(title_tv);
        List<ExpertRecorderDetailModel.DataBean.ExpertVoicesBean> expertVoices = dataBean.getExpertVoices();
        if(expertVoices!=null&&expertVoices.size()>0){
            editText.setText(expertVoices.get(expertVoices.size()-1).getContent());
            recorder_content = expertVoices.get(expertVoices.size()-1).getContent();
            voiceUuid =expertVoices.get(expertVoices.size()-1).getUuid();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.next:
                showText();
                break;
        }
    }

    /**
     * 查看文字
     */
    public void showText() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context).setCancelable(true);  //先得到构造器
        builder.setTitle("提示"); //设置标题
        builder.setMessage("是否需要重新录音？"); //设置内容
        builder.setPositiveButton("不录，直接提交", new DialogInterface.OnClickListener() { //设置确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                isRecord = false;
                getData(isRecord);
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("重新录音", new DialogInterface.OnClickListener() { //设置取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                isRecord = true;
                getData(isRecord);
                dialog.dismiss();
            }
        });
        //参数都设置完成了，创建并显示出来
        builder.create().show();
    }

    public void getData(boolean isRecord) {
        title_tv = title.getText().toString().trim();
        recorder_content = editText.getText().toString().trim();
        if (TextUtils.isEmpty(title_tv)) {
            XToast.showToast(context, "请输入标题");
            return;
        } else if (TextUtils.isEmpty(recorder_content)) {
            XToast.showToast(context, "请输入录音文本");
            return;
        }

        if (isRecord) {
            model.setRecorder_content(recorder_content);
            model.setRecorder_title(title_tv);
            model.setQuestionUuid(questionUuid);
            model.setVoiceUuid(voiceUuid);
            IntentManager.goAdditionalEditRecordActivity(AdditionalEditActivity.this, model);
        } else {
            recorderModel.setUuid(questionUuid);
            recorderModel.setVoiceText(recorder_content);
            recorderModel.setContent(title_tv);
            recorderModel.setVoiceId(voiceUuid);
            showProgressDialog();
            HttpUtil.doPost(Constants.Url.EXPERT_RECORDER_EDIT, recorderModel, new HttpResponse(this, RequestOkModel.class) {
                @Override
                public void onResult(Object obj) {
                    RequestOkModel requestOkModel = (RequestOkModel) obj;
                    XToast.showToast(context, "上传成功");
                    setResult(RESULT_OK);
                    finish();
                }

                @Override
                public void responseEnd() {
                    super.responseEnd();
                    cancelProgressDialog();
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            setResult(RESULT_OK);
            finish();
        }
    }
}
